package Test;

import java.util.ArrayList;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: Test
 * @Author: song
 * @CreateTime: 2024-09-04  10:43
 * @Description: 最长公共子字符串
 * @Version: 1.0
 */

public class LongestCommonPrefix {
    public String longestCommonPrefix(String[] str) {
        if (str.length == 0 || str == null)
            return "";
        String prefix = str[0];
        int count = str.length;
        for (int i = 1; i < count; i++) {
            prefix = longestCommonPrefix(prefix,str[i]);
            if (prefix.length() == 0)
                break;
        }
        return prefix;
    }
    public String longestCommonPrefix(String str1, String str2){
        int length = Math.min(str1.length(),str2.length());
        int index = 0;
        while(index < length && str1.charAt(index) == str2.charAt(index)){
            index++;
        }
        return  str1.substring(0,index);
    }
}